package DataStructure.Tree;

public class ArrBinaryTreeDemo {
    public static void main(String[] args) {
        int[] arr = new int[]{1, 2, 3, 4, 5, 6, 7};
        new ArrBinaryTree(arr).preOrder();
    }
}
class ArrBinaryTree{
    private int[] arr;

    public ArrBinaryTree(int[] arr) {
        this.arr = arr;
    }

    public void preOrder() {this.preOrder(0);
    }
    public void preOrder(int index) {
        //输出根
        if (arr==null||arr.length==0) {
            System.out.println("二叉树为空");
            return;
        }
        System.out.println(arr[index]);
        //遍历左，如果你让index的话，那么2index绝对越界
        if (2 * index + 1<arr.length)
        preOrder(2 * index + 1);
        //遍历右
        if (2 * index + 2<arr.length)
        preOrder(2 * index + 2);
    }

    public void infixOrder() {this.infixOrder(0);
    }
    public void infixOrder(int index) {
        //输出根
        if (arr==null||arr.length==0) {
            System.out.println("二叉树为空");
            return;
        }
        //遍历左，如果你让index的话，那么2index绝对越界
        if (2 * index + 1<arr.length)
            preOrder(2 * index + 1);
        System.out.println(arr[index]);
        //遍历右
        if (2 * index + 2<arr.length)
            preOrder(2 * index + 2);
    }

    public void postOrder() {
        this.postOrder(0);
    }
    public void postOrder(int index) {
        //输出根
        if (arr==null||arr.length==0) {
            System.out.println("二叉树为空");
            return;
        }
        //遍历左，如果你让index的话，那么2index绝对越界
        if (2 * index + 1<arr.length)
            preOrder(2 * index + 1);
        //遍历右
        if (2 * index + 2<arr.length)
            preOrder(2 * index + 2);
        System.out.println(arr[index]);
    }
}
